<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/28 0028
 * Time: 下午 2:42
 */

namespace app\sports_admin\model;


use think\Model;
use traits\model\SoftDelete;

class ConstitutionProject extends Model
{
    use SoftDelete;
    protected $table = 'constitution_project';

    protected $autoWriteTimestamp = true;

    protected $pk = 'id';

    protected $hidden = ['create_time','update_time','delete_time'];

    //体质类别
    public function type()
    {
        return $this->belongsTo('constitution_type','type_id','id')->field('id,title');
    }
    //年级
    public function grade()
    {
        return $this->belongsTo('grade','grade_id','id')->field('id,grade_title');
    }
    /**
     * 列表
     * @param array $info
     */
    public static function projectList($info)
    {
        $rows = self::with('type,grade')->where(function ($query)use($info){
            if($info['type_id']){
                $query->where('type_id',$info['type_id']);
            }
            if($info['grade_id']){
                $query->where('grade_id',$info['grade_id']);
            }
        })->order('grade_id','asc')->limit($info['rows'])->page($info['page'])
            ->group('type_id,grade_id,sex,year')->select();
        $total = self::with('type,grade')->where(function ($query)use($info){
            if($info['type_id']){
                $query->where('type_id',$info['type_id']);
            }
            if($info['grade_id']){
                $query->where('grade_id',$info['grade_id']);
            }
        })->group('type_id,grade_id,sex,year')->count();
        return ['rows'=>$rows,'total'=>$total];
    }
}